package com.my.service.Impl;

import com.my.entity.Dept;
import com.my.mapper.DeptMapper;
import com.my.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;

    @Override
    public List<Dept> getDepts() {
        // 1.调用mapper的方法获取列表数据
        List<Dept> list = deptMapper.list();
        return list;
        // 2.返回数据
    }

    /**
     * 根据Id删除部门
     * @param id
     */
    @Override
    public void delete(Integer id) {
        // 调用mapper的删除方法
        Integer row = deptMapper.deptDelete(id);
        System.out.println("影响的行数 = " + row);
    }

    /**
     * 增加部门
     *
     */
    @Override
    public void addDept(Dept dept) {
        // 补全参数
        dept.setUpdateTime(LocalDateTime.now());
        dept.setCreateTime(LocalDateTime.now());
        deptMapper.addDept(dept);
    }

    @Override
    public Dept updateDept(Dept dept) {
        // 1.补充数据
        System.out.println(dept.getName());
        dept.setUpdateTime(LocalDateTime.now());
        // 2.调用mapper方法
        deptMapper.updateDept(dept);
        return dept;
    }

    @Override
    public Dept getDeptById(Integer id) {
        return deptMapper.getDeptById(id);
    }


}
